package com.boang.controller;

import com.boang.domain.Admin;
import com.boang.domain.AdminRole;
import com.boang.domain.Role;
import com.boang.service.AdminRoleService;
import com.boang.service.AdminService;
import com.boang.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/*
  @author: 13813
  @date: 2024/8/12 15:22
*/
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminRoleService adminRoleService;

    @PreAuthorize("hasRole('sys')")
    @RequestMapping("/getAdminList")
    public String getAdminList(ModelMap modelMap) {
        List<Admin> list = adminService.list();
        modelMap.put("adminList", list);
        return "admin_list";
    }

    @RequestMapping("/addAdmin")
    public String addAdmin(Admin admin) {
        //获取用户输入的密码
        String password = admin.getPassword();
        //加密对象
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        //加密
        String encode = encoder.encode(password);
        //把加密后的密码设置到Admin对象
        admin.setPassword(encode);
        //存储到数据库中
        adminService.save(admin);
        return "login";
    }

    @PreAuthorize("hasRole('sys')")
    @RequestMapping("/setRole")
    public String setRole(Admin admin, ModelMap modelMap) {
        //用户信息
        Admin ad = adminService.getById(admin.getId());
        modelMap.put("admin", ad);
        //角色信息
        List<Role> list = roleService.list();
        modelMap.put("roleList", list);
        return "admin_role";
    }

    @PreAuthorize("hasRole('sys')")
    @RequestMapping("/saveAdminRole")
    public String saveAdminRole(Admin admin, ModelMap modelMap) {

        //页面中选中的角色ID集合
        List<Integer> roleIds = admin.getRoleIds();

        //创建集合存储AdminRole对象
        ArrayList<AdminRole> adminRoleList = new ArrayList<>();
        roleIds.forEach(roleId -> {
            //admin_role这张中间表对应的实体类对象
            AdminRole adminRole = new AdminRole();
            //给对象设置角色id
            adminRole.setRoleId(roleId);
            //设置用户ID
            adminRole.setAdminId(admin.getId());
            //把对象装到集合中
            adminRoleList.add(adminRole);
        });

        //删除根据用户ID
        adminRoleService.removeAdminRoleByAdminId(admin.getId());

        //批量存储AdminRole对象到数据库
        adminRoleService.saveBatch(adminRoleList);


        List<Admin> list = adminService.list();
        modelMap.put("adminList", list);
        return "admin_list";
    }
}
